package main

import (
	"fmt"
	"sync"
	"time"
)

// const (
//
//	COUNT int = 10
//
// )
var resChan = make(chan int, 1) // 使用缓冲通道
func main() {
	var wg sync.WaitGroup

	//str := "Hello word"
	//for i := 0; i < COUNT; i++ {
	//	println(i, "123")
	//}
	//print(str)
	wg.Add(1)
	go func() {
		defer wg.Done()
		time.Sleep(time.Millisecond * 2)
		resChan <- 1

	}()
	go func() {
		defer close(resChan) // 在处理完所有数据后关闭通道
		test()
	}()
	test()
	wg.Wait()
	//// 从通道中读取数据
	//select {
	//case data := <-resChan:
	//	doData(data)
	//default:
	//	fmt.Println("no data received")
	//}
}

// do request
func test() {
	select {
	case data := <-resChan:
		doData(data)
	case <-time.After(time.Millisecond * 2):
		fmt.Println("request time out")
	}
}

func doData(v int) {
	fmt.Printf("接收到的data值： %d\n", v)
}
